package site.psvm.phoneinfo.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.cache.CacheProperties;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import site.psvm.phoneinfo.common.bean.dto.ListDto;
import site.psvm.phoneinfo.common.bean.mongoModel.PhoneInfoModel;
import site.psvm.phoneinfo.common.util.MongoUtil;

import java.util.List;

@Service
public class PhoneInfoService {

    private final MongoTemplate mongoTemplate;
    private final StringRedisTemplate redisTemplate;

    private static final Logger logger = LoggerFactory.getLogger(PhoneInfoService.class);

    @Autowired
    public PhoneInfoService(MongoTemplate mongoTemplate, StringRedisTemplate redisTemplate) {
        this.mongoTemplate = mongoTemplate;
        this.redisTemplate = redisTemplate;
    }

    //查询最新的10条手机信息
    public ListDto<PhoneInfoModel> queryRecentUpdatesPhoneInfo() {
        try{
            Query query = MongoUtil.getPageSortQuery(0, 10, false, "createTime");
            List<PhoneInfoModel> phoneInfoModels = mongoTemplate.find(query, PhoneInfoModel.class);
            return new ListDto<>().ok(phoneInfoModels);
        }catch(Exception e){
            logger.error("PhoneInfoService,listPhoneInfo,error,",e);
            return new ListDto<>().error();
        }
    }
}
